package com.alkaid.anime.controller;

import com.alkaid.anime.service.CollectService;
import com.alkaid.commons.controller.BaseController;
import com.alkaid.commons.entity.Anime;
import com.alkaid.commons.entity.Collect;
import com.alkaid.commons.utils.JsonResult;
import com.alkaid.commons.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author Kylin
 */
@RestController
@RequestMapping("api/anime")
public class CollectController extends BaseController {

    private CollectService collectService;

    @Autowired
    public void setCollectService(CollectService collectService) {
        this.collectService = collectService;
    }

    @GetMapping("/collect")
    public JsonResult<Boolean> getCollect(HttpServletRequest request, @Validated Collect collect) {
        String jwt = request.getHeader("Authorization");
        JwtUtils jwtUtils = new JwtUtils();
        jwtUtils.parseJwt(jwt);
        Long userId = jwtUtils.getUser().getId();
        collect.setUserId(userId);
        Boolean result = collectService.getCollect(collect);
        return new JsonResult<>(200, result, "success");
    }

    @PostMapping("/collect")
    public JsonResult<Void> setCollect(HttpServletRequest request, @Validated Collect collect) {
        String jwt = request.getHeader("Authorization");
        JwtUtils jwtUtils = new JwtUtils();
        jwtUtils.parseJwt(jwt);
        Long userId = jwtUtils.getUser().getId();
        collect.setUserId(userId);
        collectService.setCollect(collect);
        return new JsonResult<>(200, null, "success");
    }

    @GetMapping("/get_collect_list")
    public JsonResult<List<Anime>> getCollectList(HttpServletRequest request) {
        String jwt = request.getHeader("Authorization");
        JwtUtils jwtUtils = new JwtUtils();
        jwtUtils.parseJwt(jwt);
        Long userId = jwtUtils.getUser().getId();
        List<Anime> animeList = collectService.getCollectList(userId);
        return new JsonResult<>(200, animeList, "success");
    }
}
